STDOUT.sync = true

# get rails root
RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + "/../..")

LOCAL_CONF = File.read(File.join(RAILS_ROOT, "config/initializers/conf-local.rb"))

# get rake path from local-config
RAKE_PATH = LOCAL_CONF.match(/rake_path\s*=\s*"(.+)"/)[1]

# get crawler PID file
pid_file = File.join(RAILS_ROOT, "tmp/crawler_pid")
pid = File.read(pid_file) rescue ""

# if crawler is running, say so
if begin Process.kill(0, pid.to_i) == 1 rescue false end
  #puts "Crawler is already running."

# otherwise, fire it up
else
  puts "[Bootstrapper] Crawler not running. Restarting it."
  
  # change to rails root
  Dir.chdir(RAILS_ROOT)
  
  # run the rake command
  Process.exec("bundle exec rake --trace crawler:go RAILS_ENV=#{ARGV[0] || 'development'}")
end
